1 00:00:01,050 --> 00:00:01,760 All righty. 2 00:00:01,770 --> 00:00:06,990 Now that we know the different loops in Lua, I want to go ahead and have us practice with them a little 3 00:00:06,990 --> 00:00:07,470 bit. 4 00:00:07,500 --> 00:00:11,040 We're also going to be learning about two new keywords in Lua as well. 5 00:00:11,040 --> 00:00:15,870 And these are going to be the break and the continue keywords. 6 00:00:16,020 --> 00:00:21,750 The break keyword allows for us to exit or break out of a loop prematurely. 7 00:00:21,780 --> 00:00:28,140 This keyword is useful for whenever we have a loop that needs to exit when some condition is met. 8 00:00:28,170 --> 00:00:33,300 So to demonstrate this break keyword, what I'm going to do is create an infinite loop. 9 00:00:33,300 --> 00:00:38,070 So while true or we're going to do is create a new scope here. 10 00:00:38,070 --> 00:00:42,600 And I'm going to create a variable outside of this loop, I'm just going to call it I and set it to 11 00:00:42,600 --> 00:00:44,220 the value of zero. 12 00:00:44,220 --> 00:00:47,760 And I'm going to do inside of this while loop is increment I by one. 13 00:00:47,760 --> 00:00:51,510 So we could use the plus equal operator and put our value of one here. 14 00:00:52,500 --> 00:01:00,210 And if the value of AI goes beyond, let's say, the value of ten, then what I want to do is exit out 15 00:01:00,210 --> 00:01:01,710 of this infinite while loop. 16 00:01:01,710 --> 00:01:04,710 And this is where we can use the break keyword. 17 00:01:04,830 --> 00:01:10,980 So when the interpreter encounters this break keyword, it's going to exit out of this while loop and 18 00:01:10,980 --> 00:01:12,930 then continue down into our script. 19 00:01:12,930 --> 00:01:17,020 Because once our interpreter hits this while loop, it's going to be stuck inside of here. 20 00:01:17,040 --> 00:01:21,540 The main thread is going to be stuck inside of here until it has the opportunity to exit. 21 00:01:21,870 --> 00:01:27,120 And when this condition evaluates to true, we are able to hit this break keyword hop out of the while 22 00:01:27,120 --> 00:01:29,820 loop and continue executing down our script. 23 00:01:30,450 --> 00:01:35,700 So this can be useful in many different circumstances when you're trying to script game logic, and 24 00:01:35,700 --> 00:01:40,830 you might have a while loop or a loop that executes forever, and you need to be able to exit out of 25 00:01:40,860 --> 00:01:43,170 that loop when a certain condition is met. 26 00:01:43,530 --> 00:01:46,800 So what I'm also going to put in here is a simple wait statement. 27 00:01:46,800 --> 00:01:52,500 And actually I'll put it up top here so we can like wait for one second. 28 00:01:52,830 --> 00:01:57,000 And then I'm just going to print out I into the console. 29 00:01:57,210 --> 00:01:59,280 And then we can go ahead and run our game. 30 00:02:00,290 --> 00:02:03,260 And as you can see, our loop is executing every single second. 31 00:02:03,260 --> 00:02:08,150 And eventually, once the value exceeds ten, our loop is going to break. 32 00:02:09,000 --> 00:02:11,310 So here's ten and there we go. 33 00:02:11,310 --> 00:02:15,360 We have stopped executing when I was incremented to 11. 34 00:02:15,390 --> 00:02:17,400 That condition evaluated to true. 35 00:02:17,400 --> 00:02:19,440 And we broke out of that loop. 36 00:02:19,650 --> 00:02:24,060 So when I hit ten and then we incremented I here to 11. 37 00:02:24,090 --> 00:02:28,500 We evaluated it inside of here and realized that 11 is greater than ten. 38 00:02:28,500 --> 00:02:30,030 And then we hit this break statement. 39 00:02:30,030 --> 00:02:36,150 And then we exited out of the while loop, meaning we ignored the rest of the code below the break statement. 40 00:02:36,270 --> 00:02:40,230 Pretty simple and very nice to have when you're scripting in loops. 41 00:02:40,320 --> 00:02:46,290 Now the other keyword is the continue keyword, and this one allows us to continue to the next iteration 42 00:02:46,290 --> 00:02:48,780 of the loop and ignore everything else below it. 43 00:02:48,810 --> 00:02:53,700 This keyword is useful for when we have to, let's say, search through a set of data, and we only 44 00:02:53,700 --> 00:02:57,260 want to affect certain data inside of it based on a condition. 45 00:02:57,270 --> 00:03:02,220 Maybe we only want to affect positive numbers or certain data types and so on. 46 00:03:02,250 --> 00:03:07,080 This is where we can evaluate for a condition, and if the condition does not evaluate to true, we 47 00:03:07,080 --> 00:03:11,430 ignore it and continue to the next value using the continue keyword. 48 00:03:11,460 --> 00:03:15,130 So to demonstrate that what I'm going to do is create a table. 49 00:03:15,150 --> 00:03:16,920 I'm going to call it my array. 50 00:03:17,310 --> 00:03:22,650 And I'm just going to store some numbers in here like one, two, three, four and five. 51 00:03:22,650 --> 00:03:25,080 And then we can go ahead and loop through this table. 52 00:03:25,380 --> 00:03:27,540 I do not care about the index only the values. 53 00:03:27,540 --> 00:03:35,100 So I'll create an underline for the index variable and then put v here for the value in ipairs pass 54 00:03:35,100 --> 00:03:36,030 my array. 55 00:03:36,890 --> 00:03:42,740 And what I want to do is I want to, let's say, only print out numbers inside of this table that are 56 00:03:42,740 --> 00:03:43,460 even. 57 00:03:43,580 --> 00:03:48,830 So in order to do that, we have to use an operator in Lua called the modulus operator. 58 00:03:48,830 --> 00:03:54,160 And this is a simple operator where it just basically divides your number by the number you supply. 59 00:03:54,170 --> 00:03:57,000 And if there's any leftovers, that's what gets returned. 60 00:03:57,020 --> 00:04:03,320 So for example we can check if v and then we use the modulus operator which is the percent sign. 61 00:04:03,320 --> 00:04:04,580 And then we can put two. 62 00:04:04,580 --> 00:04:07,250 And we can check to see if this is equal to zero. 63 00:04:07,640 --> 00:04:15,740 If this is equal to zero that means it is even because if the value can be divided by two and has a 64 00:04:15,740 --> 00:04:20,430 leftover value of zero, that means we perfectly fit two inside of our value. 65 00:04:20,450 --> 00:04:23,930 If our value was, let's say three. 66 00:04:23,960 --> 00:04:30,320 Well, we can only divide three by two once, and the leftover value would be one, which means it is 67 00:04:30,320 --> 00:04:31,690 not an even value. 68 00:04:31,700 --> 00:04:34,790 But in this case, we only want to print out values that are even. 69 00:04:34,790 --> 00:04:42,590 So what I'm going to do here is that if this value using the modulus operator by two is not equal to 70 00:04:42,590 --> 00:04:48,410 zero, which means it's an odd number, then we want to ignore this value and go to the next value that's 71 00:04:48,410 --> 00:04:49,610 inside of our table. 72 00:04:49,640 --> 00:04:53,270 So inside of this if statement we can use the continue keyword. 73 00:04:53,270 --> 00:04:56,870 And that means everything below the continue keyword is going to be ignored. 74 00:04:56,870 --> 00:05:00,500 And we're going to move on to the next iteration of our loop. 75 00:05:00,710 --> 00:05:04,580 So down here, all I'm going to do is simply print V into the console. 76 00:05:04,580 --> 00:05:13,670 And then I'm going to go ahead and comment out this code here by using control and then slash to comment 77 00:05:13,670 --> 00:05:15,200 out my entire block here. 78 00:05:15,200 --> 00:05:17,120 So that way we don't have to wait for that loop. 79 00:05:17,120 --> 00:05:19,310 And then we can go ahead and hit run. 80 00:05:21,300 --> 00:05:26,730 And as you can see, only value of two and four was printed out inside of the console because those 81 00:05:26,730 --> 00:05:30,330 were the only even numbers inside of our table. 82 00:05:31,250 --> 00:05:36,770 All the other values were ignored because they were not even numbers, and we moved on to the next iteration 83 00:05:36,770 --> 00:05:39,710 of the loop thanks to our continue keyword. 84 00:05:40,040 --> 00:05:45,290 So with this newly learned knowledge, let's go ahead and practice with a couple of activities I would 85 00:05:45,290 --> 00:05:47,060 like for you to complete. 86 00:05:47,180 --> 00:05:54,500 The first activity is this I want you to create an infinite loop that increments a variable by one in 87 00:05:54,500 --> 00:05:55,580 each iteration. 88 00:05:55,580 --> 00:05:58,220 And I want you to only print out even numbers. 89 00:05:58,220 --> 00:06:03,980 If a number is odd, you're going to use the continue keyword to ignore it, and you're going to use 90 00:06:03,980 --> 00:06:06,770 the modulo or modulus operator to help you out. 91 00:06:07,100 --> 00:06:09,140 So this is going to be your infinite while loop. 92 00:06:09,140 --> 00:06:15,410 That's just going to constantly increment and only print out positive or I mean even values. 93 00:06:15,410 --> 00:06:20,510 Since we just did that up here, this should be a very easy activity for you to complete. 94 00:06:20,510 --> 00:06:26,120 So go ahead, practice doing it right now and then you can go ahead and come back and see my solution. 95 00:06:27,840 --> 00:06:28,290 Alrighty. 96 00:06:28,290 --> 00:06:30,420 So my solution would be this. 97 00:06:30,450 --> 00:06:35,190 We're going to create a variable outside of the while loop, and I'm just going to call it I and set 98 00:06:35,190 --> 00:06:36,270 it equal to zero. 99 00:06:36,300 --> 00:06:38,250 Then we're going to create the while loop. 100 00:06:38,250 --> 00:06:41,640 So while true do here's my infinite while loop. 101 00:06:41,640 --> 00:06:45,150 And inside of here what I want to do is increment I by one. 102 00:06:45,150 --> 00:06:46,770 So plus equal one. 103 00:06:46,980 --> 00:06:55,410 And then I want to check if I modulus two is not equal to zero, which means that this is an odd number. 104 00:06:55,410 --> 00:07:00,540 If it is then we're going to continue and move on to the next iteration of the loop. 105 00:07:00,630 --> 00:07:04,230 Otherwise we can go ahead and simply print out I into the console. 106 00:07:04,380 --> 00:07:09,840 Here we have our infinitely executing loop, and since it is an infinitely executing loop, that means 107 00:07:09,840 --> 00:07:11,030 we must yield. 108 00:07:11,040 --> 00:07:14,760 So at the top I'm just going to add a simple yield function up here. 109 00:07:14,760 --> 00:07:17,190 And we'll just yield for like let's say one second. 110 00:07:18,000 --> 00:07:20,670 Then we can go ahead and run our game. 111 00:07:23,110 --> 00:07:26,320 And as you can see, we get two and then we get four. 112 00:07:27,090 --> 00:07:30,510 And through each iteration of our loop, we're waiting one second, right? 113 00:07:30,510 --> 00:07:35,850 So we're actually waiting two seconds between these different values, because the values that we're 114 00:07:35,850 --> 00:07:41,100 in between them were odd numbers, and because they were odd numbers, we're ignoring them and moving 115 00:07:41,100 --> 00:07:42,460 on to the next value. 116 00:07:42,510 --> 00:07:47,910 So if you don't want to wait two seconds in between values, then what we could do instead. 117 00:07:49,330 --> 00:07:51,520 As we could add our weight statement. 118 00:07:52,110 --> 00:07:53,160 At the bottom. 119 00:07:53,490 --> 00:08:00,660 So this way, whenever we encounter a value that is not even, then we're going to immediately skip 120 00:08:00,660 --> 00:08:03,870 to the next iteration without having to yield for one second. 121 00:08:03,900 --> 00:08:10,410 So if we go ahead and run our game again, this time every single second, now we are printing out an 122 00:08:10,410 --> 00:08:12,480 even number inside of our console. 123 00:08:13,830 --> 00:08:15,740 So great job on that first activity. 124 00:08:15,750 --> 00:08:19,470 We're now going to move on to the next activity I would like you to complete, which is going to be 125 00:08:19,470 --> 00:08:20,720 a little bit more difficult. 126 00:08:20,730 --> 00:08:24,290 So I'm just going to comment out my solution right here. 127 00:08:24,300 --> 00:08:27,060 And let's go ahead and move on to the next activity. 128 00:08:27,630 --> 00:08:31,860 In this activity, I would like for you to create a simple random dungeon game. 129 00:08:31,860 --> 00:08:36,720 I want you to create an infinite loop that generates a random number between 1 and 10. 130 00:08:36,720 --> 00:08:40,500 And you can do so by using the math dot random function to help you. 131 00:08:40,500 --> 00:08:42,240 And I'll demonstrate that in a little bit. 132 00:08:42,480 --> 00:08:48,330 So when the number that is generated by this function is equal to one, I want you to display a string 133 00:08:48,360 --> 00:08:50,250 saying the exit was found. 134 00:08:50,250 --> 00:08:53,730 And then I want you to exit out of the loop using the break keyword. 135 00:08:53,820 --> 00:08:58,110 However, when the number is equal to two, then I want you to display a string in the console that 136 00:08:58,110 --> 00:09:00,360 says the player encountered a monster. 137 00:09:00,360 --> 00:09:04,980 When the number is equal to three, I want you to display a string saying the player encountered some 138 00:09:04,980 --> 00:09:09,780 loop, and then when the number is not equal to any of those previous values, then I want you to simply 139 00:09:09,780 --> 00:09:12,870 display a string saying the player is wandering the dungeon. 140 00:09:13,080 --> 00:09:17,280 So the math dot random function real quick is a very simple function. 141 00:09:17,280 --> 00:09:23,070 It takes a minimum number and a maximum number, and it returns a random number within the range provided. 142 00:09:23,070 --> 00:09:27,540 So if I would like to generate a random number between 1 and 10, then I could simply just pass one 143 00:09:27,540 --> 00:09:28,290 and ten here. 144 00:09:28,290 --> 00:09:31,620 And it's going to pick a random number between those two values. 145 00:09:31,620 --> 00:09:37,170 And of course you can store the return of this function inside of a variable like random int. 146 00:09:37,410 --> 00:09:40,740 And this is what you're going to be using for your infinite while loop. 147 00:09:40,740 --> 00:09:46,890 So go ahead, take a minute or two to create your solution and then come back to see my solution. 148 00:09:48,640 --> 00:09:48,960 All righty. 149 00:09:49,030 --> 00:09:52,690 My solution for this problem is first we need to create our while loop. 150 00:09:52,690 --> 00:09:54,520 So while true do. 151 00:09:54,790 --> 00:09:57,940 And then inside of here we need to generate our random number. 152 00:09:57,940 --> 00:10:01,900 So I can create a variable I'll call it random int. 153 00:10:01,900 --> 00:10:04,360 And this is equal to math dot random. 154 00:10:04,360 --> 00:10:07,570 And the range is going to be from one up to ten. 155 00:10:08,350 --> 00:10:14,110 And now we can go ahead and create our if statement section, so we can check if our random integer 156 00:10:14,200 --> 00:10:15,550 is equal to one. 157 00:10:15,550 --> 00:10:24,010 If it is, then we can print out inside of the console the player found the exit, and since we have 158 00:10:24,010 --> 00:10:29,800 reached or attained the value of one, we want to go ahead and exit out of this forever loop so we can 159 00:10:29,800 --> 00:10:31,540 use the break keyword. 160 00:10:31,540 --> 00:10:34,060 And now we have exited out of our while loop. 161 00:10:35,020 --> 00:10:42,130 Else if our random integer let's say it's equal to two, then we need to print into the console that 162 00:10:42,130 --> 00:10:43,570 we encountered a monster. 163 00:10:43,570 --> 00:10:44,770 So we can print. 164 00:10:44,800 --> 00:10:49,150 The player encountered a monster. 165 00:10:50,410 --> 00:10:52,480 However, if the value. 166 00:10:53,480 --> 00:10:54,860 Is equal to three. 167 00:10:55,070 --> 00:10:59,840 Then what we need to print out is that the player found some loop. 168 00:11:01,100 --> 00:11:06,830 And last but not least, if the value randomly generated here does not match any of these previous conditions, 169 00:11:06,830 --> 00:11:13,310 then we can use the else statement and create a new block down here and we'll just print into the console. 170 00:11:13,340 --> 00:11:16,640 The player is wandering the dungeon. 171 00:11:17,910 --> 00:11:22,830 And since we don't know how long this loop will execute for, I'm also going to add a wait statement 172 00:11:22,830 --> 00:11:27,240 at the end so we can wait for, let's say, one second once more. 173 00:11:27,570 --> 00:11:29,890 And now this should be our solution complete. 174 00:11:29,910 --> 00:11:34,050 So let's go ahead and run our game and see what gets printed into the console. 175 00:11:36,860 --> 00:11:37,570 There we go. 176 00:11:37,580 --> 00:11:38,480 Players wandering. 177 00:11:38,480 --> 00:11:40,070 We've encountered a monster. 178 00:11:40,730 --> 00:11:42,230 We've continued to wander. 179 00:11:42,230 --> 00:11:44,390 And then eventually we found the exit. 180 00:11:44,390 --> 00:11:50,840 So our math dot random function finally generated the number of one while generating three four, five 181 00:11:50,840 --> 00:11:53,420 previous values that were not equal to one. 182 00:11:53,750 --> 00:12:00,170 And of course, we can stop the test and run it again, and this time it'll print out some different 183 00:12:00,170 --> 00:12:01,010 statements. 184 00:12:01,010 --> 00:12:03,620 So this time it executed much shorter. 185 00:12:03,650 --> 00:12:08,900 It only had two previous values, which was the players wandering the dungeon, and then the player 186 00:12:08,900 --> 00:12:10,000 found the exit. 187 00:12:10,010 --> 00:12:14,120 So we can go ahead and stop this again and hit run once more. 188 00:12:15,440 --> 00:12:17,360 And immediately this one got pretty lucky. 189 00:12:17,360 --> 00:12:19,550 We had a 10% chance of finding the exit. 190 00:12:19,550 --> 00:12:20,450 And look at that. 191 00:12:20,450 --> 00:12:22,250 The player found the exit. 192 00:12:22,280 --> 00:12:27,770 Now, if we would like to decrease the odds of our player being able to find the exit, then we can 193 00:12:27,770 --> 00:12:30,260 increase this range to like 100. 194 00:12:30,500 --> 00:12:34,280 And if the value is equal to one, then the player finds the exit. 195 00:12:34,460 --> 00:12:35,630 Otherwise we can do. 196 00:12:35,630 --> 00:12:42,140 Here is we can check if the random integer is greater than or equal to two. 197 00:12:42,230 --> 00:12:48,290 And this random integer is let's say less than or equal to 15. 198 00:12:48,320 --> 00:12:52,700 If that's the case, then we can print out the player has encountered a monster. 199 00:12:53,030 --> 00:13:02,210 Otherwise we can check if our value is greater than 15, and also if our value is, let's say less than 200 00:13:02,210 --> 00:13:04,220 or equal to 30. 201 00:13:04,340 --> 00:13:07,790 If that's the case, then we can print out that the player has found some loot. 202 00:13:08,030 --> 00:13:12,620 Otherwise, if the value randomly generated here does not match any of the previous ones, then we're 203 00:13:12,620 --> 00:13:15,050 just going to say the player is wandering the dungeon. 204 00:13:15,050 --> 00:13:20,060 So now if we go ahead and run our game this time, our loop should last a lot longer. 205 00:13:20,150 --> 00:13:22,670 So as you can see, we are wandering the dungeon. 206 00:13:23,450 --> 00:13:26,940 Eventually we found some loot and were continuing to wander. 207 00:13:26,960 --> 00:13:29,480 We've encountered a monster. 208 00:13:29,690 --> 00:13:31,790 We are continuing to wander. 209 00:13:32,400 --> 00:13:36,150 And we can go ahead and see how long this will eventually take us to find the exit. 210 00:13:37,590 --> 00:13:39,810 But so far, we're still wandering our dungeon. 211 00:13:40,860 --> 00:13:42,810 Alrighty after quite a bit of time. 212 00:13:42,810 --> 00:13:44,940 Maybe that was 2 or 3 minutes. 213 00:13:44,970 --> 00:13:50,310 Our player has finally found the exit because through each iteration of our loop, our player only had 214 00:13:50,310 --> 00:13:52,780 a 1% chance of finding the exit. 215 00:13:52,800 --> 00:13:55,080 So as you can see, a lot of time has elapsed. 216 00:13:55,110 --> 00:14:01,350 The player was wandering the dungeon, finding loot and countering monsters, and eventually we finally 217 00:14:01,350 --> 00:14:04,680 found the exit and we broke out of our infinite loop. 218 00:14:05,240 --> 00:14:10,910 I hope you enjoyed these two simple practice activities and learning about the break and continue keywords. 219 00:14:10,940 --> 00:14:14,060 Keep up the good work and I will see you in the next lecture.